//
//  SISSCommonRentCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/14.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCommonRentCell.h"

@interface SISSCommonRentCell ()

/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSCommonRentCell

- (void)setRentOutModel:(SISSRentOutModel *)rentOutModel {
    _rentOutModel = rentOutModel;
    
    [self.recommendButton setTitle:@"推荐租客" forState:UIControlStateNormal];
    self.topLabel.text = [NSString stringWithFormat:@"%@ | %@元",rentOutModel.neighbourhood,rentOutModel.monthlyRent];
    self.middleLabel.text = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@ ",rentOutModel.region,rentOutModel.regionDetail,rentOutModel.area,rentOutModel.houseType];
    
//    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    NSArray *houseFeatureArr = rentOutModel.houseFeature.length == 0 ? @[] : [rentOutModel.houseFeature componentsSeparatedByString:@"#"];
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@"%@",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (rentOutModel.houseFeature.length == 0) {
            make.height.mas_equalTo(1);
        } else {
            make.height.mas_equalTo(self.markLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
        
    }];
}

- (void)setIsShowFullName:(BOOL)isShowFullName {
    _isShowFullName = isShowFullName;
    self.topLabel.text = self.tenantModel.renterName;
}

- (void)setTenantModel:(SISSTenantModel *)tenantModel {
    _tenantModel = tenantModel;
    [self.recommendButton setTitle:@"推荐租房" forState:UIControlStateNormal];
    NSString *sexStr = tenantModel.sex == 1 ? @"先生" : @"女士";
    self.topLabel.text = self.isShowFullName ? tenantModel.renterName : [NSString stringWithFormat:@"%@%@  求租",[tenantModel.renterName substringToIndex:1],sexStr];
//    self.topLabel.text = [NSString stringWithFormat:@"%@%@  求租",[tenantModel.renterName substringToIndex:1],sexStr];
    self.middleLabel.text = [NSString stringWithFormat:@"%@-%@元 | %@-%@ | %@",tenantModel.minPrice,tenantModel.maxPrice,tenantModel.region,tenantModel.regionDetail,tenantModel.houseType];
//    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    NSArray *houseFeatureArr = tenantModel.houseFeature.length == 0 ? @[] : [tenantModel.houseFeature componentsSeparatedByString:@"#"];
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.text = [NSString stringWithFormat:@"%@",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
    }
    [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (tenantModel.houseFeature.length == 0) {
            make.height.mas_equalTo(1);
        } else {
            make.height.mas_equalTo(self.markLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
    }];
}


- (void)setIsHideButton:(BOOL)isHideButton {
    _isHideButton = isHideButton;
    self.recommendButton.hidden = isHideButton;
}

/**
 点击推荐房源按钮
 */
- (void)recommendButtonClickAction:(UIButton *)sender {
    
    // 送出代理
    if ([self.delegate respondsToSelector:@selector(commonRentCellRecommendButtonDidClick:)]) {
        
        [self.delegate commonRentCellRecommendButtonDidClick:sender];
    }
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *topLabel = [[UILabel alloc] init];
    topLabel.font = [UIFont systemFontOfSize:14];
    topLabel.textColor = MAIN_TEXT_COLOR;
    topLabel.text = @"陆先生 求购";
    [self.contentView addSubview:topLabel];
    self.topLabel = topLabel;
    
    // 详细信息
    UILabel *middleLabel = [[UILabel alloc] init];
    middleLabel.font = [UIFont systemFontOfSize:14];
    middleLabel.textColor = MAIN_TEXT_COLOR;
    middleLabel.text = @"300-500万 | 南山区-西丽 | 3房";
    [self.contentView addSubview:middleLabel];
    self.middleLabel = middleLabel;
    
    UIView *labelsView = [[UIView alloc] init];
    [self.contentView addSubview:labelsView];
    self.labelsView = labelsView;
    
    NSMutableArray<UILabel *> *arrM = [NSMutableArray arrayWithCapacity:6];
    float labelX = 20;
    float labelY = 0;
    CGFloat labelWidth = 52;
    for (NSInteger i = 0; i < 6; i++) {
        if (labelX + labelWidth > SCREEN_WIDTH) {
            labelX = 20;
            labelY += 30;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 16)];
        label.textColor = SECONDARY_TEXT_COLOR;
        label.font = [UIFont systemFontOfSize:11];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"房屋特色";
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
//        label.layer.masksToBounds = YES;
        [arrM addObject:label];
        [self.labelsView addSubview:label];

        labelX = CGRectGetMaxX(label.frame) + 10;
        label.hidden = YES;
        
    }
    
    self.markLabels = arrM;
    
    // 选择按钮
    UIButton *recommendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // 设置推荐按钮的属性
    recommendButton.layer.borderWidth = 1;
    recommendButton.layer.borderColor = COLOR(0x33A3BB).CGColor;
    recommendButton.layer.cornerRadius = 4;
    recommendButton.layer.masksToBounds = YES;
    recommendButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [recommendButton setTitleColor:MAIN_COLOR forState:UIControlStateNormal];
    [recommendButton setBackgroundImage:[ImageWithColor imageWithColor:[UIColor whiteColor]] forState: UIControlStateNormal];
    [recommendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [recommendButton setBackgroundImage:[ImageWithColor imageWithColor:MAIN_COLOR] forState: UIControlStateHighlighted];
    [recommendButton setTitle:@"推荐租客" forState:UIControlStateNormal];
    [recommendButton addTarget:self action:@selector(recommendButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:recommendButton];
    self.recommendButton = recommendButton;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    self.bottomLine = lineView;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    // 自动布局
    CGFloat margin = 8;
    CGSize buttonSize = CGSizeMake(66, 28);
    WeakSelf
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(margin);
        make.left.equalTo(weakSelf.contentView).offset(margin * 2.5);
    }];
    
    [self.middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.topLabel.mas_bottom).offset(margin * 2);
        make.left.equalTo(weakSelf.topLabel);
//        make.bottom.equalTo(self.bottomLine).offset(-margin);
    }];
    
    [self.recommendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.middleLabel).offset(-margin);
        make.right.equalTo(weakSelf.contentView).offset(-margin * 2);
        make.size.mas_equalTo(buttonSize);
    }];

    [self.labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.contentView);
        make.top.equalTo(weakSelf.middleLabel.mas_bottom).offset(margin);
        make.height.mas_equalTo(66);
        make.bottom.equalTo(weakSelf.bottomLine).offset(-margin);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
    
//    for (NSInteger i = 0; i < self.markLabels.count; i++) {
//        UILabel *label = self.markLabels[i];
//        [label mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(weakSelf.labelsView).offset(margin);
//            make.left.equalTo(weakSelf.labelsView).offset(8 + 66 * i);
//            make.bottom.equalTo(self.bottomLine).offset(-8);
//        }];
//    }
}

@end
